//
//  StaisticComlainController.m
//  PropertyMananger
//
//  Created by Mansion on 17/3/5.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "StaisticComlainController.h"
#import "LeaderComplainCell.h"
#import "ComplainModel.h"

@interface StaisticComlainController ()
@property (nonatomic,strong) NSMutableArray * complainRepiarArr;//数据源

@property (nonatomic,copy) NSString * selectedTime;//选择的的时间

@end

@implementation StaisticComlainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH - 64 - 50) isGroup:NO];
    
    self.complainRepiarArr = [[NSMutableArray alloc] init];
    
   [self getYearAndMonthData];
    
    //暂无数据暂位图
    if (self.complainRepiarArr.count == 0) {
        
        UIView * lineView = [[UIView alloc]init];
        [self.tableView addSubview:lineView];
        lineView.backgroundColor = [BHColor(153, 153, 153)colorWithAlphaComponent:0.2];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(44);
            make.size.mas_equalTo(CGSizeMake(BHscreenW, 15));
        }];
        
    }
    
    
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(timeClick:) name:LeaderTimeNoticeCenter object:nil];
    
    [self createPlaceHolder];
    
    [self createRefresh];
    
    [self postStasticsComplainRepairData];
    
}
- (void)timeClick:(NSNotification *)noti{
    
    DLog(@"通知传来 === %@",noti.userInfo[@"key"]);
    self.selectedTime = noti.userInfo[@"key"];
    
}
- (void)dealloc
{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.complainRepiarArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LeaderComplainCell * cell = [LeaderComplainCell cellWithTableView:tableView];
    
    ComplainModel * model = self.complainRepiarArr[indexPath.section];
    
    cell.model = model;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 210;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
#pragma mark - 创建刷新UI
- (void)createRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPage = 1;
        
        self.isDown = YES;
        //获取数据
        [self postStasticsComplainRepairData];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.nextPage += 1;
        
        self.isDown = NO;
        //获取数据
        [self postStasticsComplainRepairData];
        
        //    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }];
}

#pragma mark - 暂未图
- (void)createPlaceHolder
{
    self.placehoderImg = [[UIImageView alloc]init];
    [self.tableView addSubview:self.placehoderImg];
    self.placehoderImg.image = [UIImage imageNamed:@"ico_complaint_no"];
    
    [self.placehoderImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.tableView.mas_bottom).with.offset(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.tableView addSubview:self.titleLabel];
    self.titleLabel.text = @"暂无投诉单";
    
    self.titleLabel.textColor= BHColor(153, 153, 153);
    self.titleLabel.font = BHFont(15);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.placehoderImg.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(100, 25));
    }];
    
}
#pragma mark - 获取数据
- (void)postStasticsComplainRepairData{
    
  NSDictionary *  parms = @{
              @"CommunityID":userModel.CommunityID,
              @"Type":@"5",
              @"Time":self.selectedTime,
              @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"findmodebykey\",\"version\":\"V1.0\"}"
              };
    DLog(@"===%@",parms);
    [HomeNetWorkRequest postLeaderFromDetailListSuccess:^(id data) {
        
        if (self.isDown == YES) {
            
            [self.complainRepiarArr removeAllObjects];
            self.isDown = NO;
        }
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"领导报表公共报修列表的数据 == %@",array);
        for (NSDictionary * dict in array) {
            
            ComplainModel * model = [[ComplainModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dict];
            
            [self.complainRepiarArr addObject:model];
            
            //   DLog(@"ProcessState == %@",model.ProcessState);
        }
        
        if (self.complainRepiarArr.count != 0){
            
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
            
        }else{
            
            self.tableView.mj_footer.hidden = YES;
            self.placehoderImg.hidden = NO;
            self.titleLabel.hidden = NO;
        }
        
        if (self.complainRepiarArr.count <= 3) {
            
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } param:parms];
    
}
#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
    self.selectedTime = timeStr;
}


@end
